package reflect_method;




import org.junit.Test;

import java.lang.reflect.Method;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class Test_ {
    //获取全部方法
    @Test
    public void getDeclaredMethods() {
        //获取类对象
        Class c = Dog.class;
        //获取类对象中全部方法
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName() + "返回值类型" + method.getReturnType() +
                    "方法参数" + method.getParameterTypes() + "参数个数" + method.getParameterCount());
        }
    }

    //获取某个方法对象
    @Test
    public void getDeclaredMethod() throws Exception {
        //获取类对象
        Class c = Dog.class;
        //获取类对象中单个方法
        Method method = c.getDeclaredMethod("eat");
        Method method1 = c.getDeclaredMethod("eat",String.class);
        System.out.println(method.getName() + " " + method.getReturnType() + " " + method.getParameterCount());
        System.out.println(method1.getName() + " " + method1.getReturnType() + " " + method1.getParameterCount());

        //暴力执行权限
        method.setAccessible(true);
        method1.setAccessible(true);
        //触发方法的执行
        Dog dog = new Dog();
        method.invoke(dog);//私有方法不能直接触发唤醒方法
        String result2 = (String) method1.invoke(dog, "骨头");
        System.out.println(result2);
    }
}
